<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use \think\Session;

class Adminuser extends CommonController
{
	/*管理员添加*/
	public function addAdminUser()
	{
		if (Request::instance()->isPost()) 
		{
			$account=input('post.account');

			$password=Request::instance()->post('password');

			$realName=Request::instance()->post('realName');
			
            $roleId=input('post.');
          
			$password=md5($password);

			$info=[
				'account' => $account,
				'password' => $password,
				'realName' => $realName
			];
        	$sql=" insert into Adminuser (`account`,`password`,`realName`)values('$account','$password','$realName')";
			$arr = Db::query($sql);
            $userId = Db::name('Adminuser')->getLastInsID();
            foreach ($roleId['roleId'] as $k => $v) 
              {
                  $newData[$k]['userId'] = $userId;
                  $newData[$k]['roleId'] = $v;
              }

			$roleArr=Db::name('userrole')->insertAll($newData);
           if ($roleArr){
          		 return $this->success('管理员添加成功','index/index');
           }
		}
		else
		{
          	$adminRole = Db::table('Adminrole')->select();
			return $this->fetch('addAdminUser',['adminRole'=>$adminRole]);
		}
	}

	/**
	 * 管理员列表
	*/
	public function listAdminUser()
	{
		$adminUserInfo = Db::table('Adminuser')->select();

		foreach ($adminUserInfo as $k => $v) 
		{
			if ($v['status'] == 1) {
				$adminUserInfo[$k]['status'] = '正常';
			} else {
				$adminUserInfo[$k]['status'] = '已冻结';
			}
		}

        return $this->fetch('listAdminUser', ['adminUserInfo'=>$adminUserInfo]);
	}

	/**
	 * 修改用户状态
	*/
	public function updateAdminUserStatus()
	{
		$userId = input('post.userId');

		$status = input('post.status');

		if ($status == '正常') 
		{
			Db::table('Adminuser')->update(['status'=>'2','userId'=>$userId]);

			echo json_encode(['error'=>0]);
		}
		else
		{
			Db::table('Adminuser')->update(['status'=>'1','userId'=>$userId]);

			echo json_encode(['error'=>1]);
		}
	}
}